<?php
/**
 *
 * 用户bll
 * @author yonggong
 *
 */

apf_require_class("Login_Ldap");
apf_require_class("Bll_BLL");
apf_require_class("Bll_Attendance_Role");

class Bll_Login_User extends BLL{

    private $ip;

    public function __construct(){
        $apf = APF::get_instance();
        $this->ip = $apf->get_request()->get_client_ip();
        $this->ip = ip2long($this->ip);
    }

    /**
     * 登录日志记录
     */
    public  function logs($action,$user_id){
        $time = time();
        switch ($action){
            case 'login':
                if (empty($this->ip)) {
                    $this->ip = 0;
                }
                $time = date('Y:m:d H:i:s');
                $this->get_loginlog_dao()->insert(array('user_id'=>$user_id,'ip'=> $this->ip,'in_time'=>$time));
                break;
            case 'logout':
                //action
                break;
        }
    }

    //根据code获取用户信息
    public function get_user_info($code){
        $user_info = $this->get_userinfo_dao()->find_row(array('code'=>$code));
        if(empty($user_info)){
            return false;
        }
        return $user_info;
    }

    /**
     * 根据用户ids获取用户信息
     * @param unknown_type $ids
     * @return multitype:|unknown
     */
    public function get_user_info_by_ids($ids){
        $result = array();
        if(empty($ids)) {
            return $result;
        }
        $user_info = $this->get_userinfo_dao()->find_by_ids($ids);
        if(empty($user_info)){
            return array();
        }
        return $user_info;
    }

    /**
     * 根据员工code获取用户的权限菜单
     * @param unknown_type $code
     */
    public function get_user_permiss_menu($code) {
        $result = array();
        if(!empty($code)) {
            $role_id = $this->get_user_role_id($code);

            //得到角色对应的app
            $bll_role = new Bll_Attendance_Role();
            $result = $bll_role->get_role_app($role_id);
        }
        return $result;
    }


    /**
     * 获得用户角色ID
     * @param unknown_type $code
     * @return string
     */
    public function get_user_role_id($code) {
        $result = '0';
        if(empty($code)) {
            return $result;
        }
        if(is_numeric($code)) {//根据用户user_id获得角色ID
            $rs = $this->get_user_role_dao()->find_by_id($code);
            if(!empty($rs)) {
                $result = $rs['role_id'];
            }
        }else {
            //获得用户信息
            $userinfo = $this->get_user_info($code);
            $user_id = (int)$userinfo['user_id'];
            if(!empty($user_id)) {
                $result = $this->get_user_role_id($user_id);
            }
        }
        return $result;
    }

    /**
     * 根据用户ids获取用户角色
     * @param unknown_type $uids
     */
    public function get_user_role($uids) {
        $result = array();
        if(empty($uids)) {return $result;}
        if(!is_array($uids)) {
            $uids = array($uids);
        }
        $uids = array_unique($uids);
        $result = $this->get_user_role_dao()->find_by_ids($uids);
        if(!empty($result)) {
            $bll_role = new Bll_Attendance_Role();
            $all_role = $bll_role->get_all_role();
            foreach($result as $key=>$v) {
                $role_name = $all_role[$v['role_id']]['name'];
                $result[$key]['role_name'] = $role_name;
            }
        }
        return $result;
    }

    /**
     *搜索用户
     */
    public function search_userinfo($where,$order = null,$limit = 0,$offset = 0) {
       if(!empty($where['role_id'])) {
           $_where = array('role_id'=>(int)$where['role_id']);
           $_where['search_keywords'] = $where['search_keywords'];
           if(!empty($where['city_name'])) {
               $_where['city_name'] = $where['city_name'];
               $total = $this->get_user_role_dao()->_find_count($_where);
               $_rs = $this->get_user_role_dao()->_find_short_assoc($_where,"user_id asc",$limit,$offset);
           }else {
               $total = $this->get_user_role_dao()->_find_count($_where);
               $_rs = $this->get_user_role_dao()->_find_short_assoc($_where,"user_id asc",$limit,$offset);
           }
           if(empty($_rs)) {return array();}
           $_userids = array();
           foreach($_rs as $v) {
             $_userids[] = $v['user_id'];
           }
           $in_list = implode(',',$_userids);
           $where["user_id in ($in_list) and 1=?"] = '1';
           unset($where['role_id']);
           $offset = 0;
       }
       unset($where['search_keywords']);
       $where['dimission_date'] = '0000-00-00 00:00:00';
       if(!isset($total)) {
          $total = $this->get_userinfo_dao()->find_count($where);
       }

       $rs = $this->get_userinfo_dao()->find_short_assoc($where,$order,$limit,$offset);
       $userids = array();
       foreach((array)$rs as $key => $v) {
          $userids[] = $v['user_id'];
          $rs[$key]['indue_date'] = substr($v['indue_date'],0,10);
       }

       if(!empty($rs)) {
           //获得用户角色
           $user_list = $this->get_user_role($userids);
           foreach($rs as $key => $v) {
                $role_name = $user_list[$v['user_id']]['role_name'];
                $rs[$key]['role_name'] = empty($role_name) ? '普通员工':$role_name;
           }
       }

        return array('data'=>$rs,'total'=>$total);
    }

    /**
     * 搜索下属信息
     */
    public function search_subordinate_userinfo($where,$order = null,$limit = 0,$offset = 0) {
        $result = array();
        if(empty($where['superior_id'])) {
            return $result;
        }
        if(empty($where['level'])) {
            $where['level'] = '1';
        }
        if($where['level']=='all') {
            unset($where['level']);
        }
        $rs = $this->get_org_relation_dao()->_find_short_assoc($where,$order,$limit,$offset);
        $total = $this->get_org_relation_dao()->_find_count($where);
        return array('data'=>$rs,'total'=>$total);
    }

    /**
     * 搜索所有用户信息  for HR
     */
    public function search_subordinate_userinfo_hr($where,$order = null,$limit = 0,$offset = 0) {
        $this->session_act();
        $result = array();
        $user_info = $this->get_user_info($_SESSION['code']);
        if($user_info['city_name'] != '上海'){
            $where = array('city_name'=>$user_info['city_name']);
        }
        $rs = $this->get_org_relation_dao()->_find_short_assoc_hr($where,$order,$limit,$offset);
        $total = $this->get_org_relation_dao()->_find_count_hr($where);
        return array('data'=>$rs,'total'=>$total);
    }


    /**
     * 搜索下属报表导出
     */
    public function search_subordinate_exp_userinfo($where,$order = null) {
        $result = array();
        if(empty($where['superior_id'])) {
            return $result;
        }
        if(empty($where['level'])) {
            $where['level'] = '1';
        }
        if($where['level']=='all') {
            unset($where['level']);
        }
        $total = $this->get_org_relation_dao()->_find_count($where);
        $rs = $this->get_org_relation_dao()->_find_short_assoc($where,$order,$total,0);
        return array('data'=>$rs,'total'=>$total);
    }

    /**
     * 搜索下属报表导出 HR
     */
    public function search_subordinate_exp_userinfo_hr($where,$order = null) {
        $result = array();
        $total = $this->get_org_relation_dao()->_find_count_hr($where);
        $rs = $this->get_org_relation_dao()->_find_short_assoc_hr($where,$order,$total,0);
        return array('data'=>$rs,'total'=>$total);
    }

    /**
     * 设置用户权限
     * @param unknown_type $role_id
     * @param unknown_type $user_list
     * @return boolean
     */
    public function set_users_role($role_id,$user_list) {
        $result = false;
        if(empty($role_id) || empty($user_list)) {
            return $result;
        }
 //       print_r($user_list);die();
        if(!is_array($user_list)) {
            $user_list = array($user_list);
        }
        //将用户数组转主userid=>userid方便操作
        $_userlist = array();
        foreach($user_list as $v) {
            $_userlist[$v] = $v;
        }
        $user_list = $_userlist;

        //获得在权限表里没有的用户列表
        //1.将已存在的用户权限做修改
        //2.未存在的做添加
        $in_userlist = $this->get_user_role($user_list);

        #1
        if(!empty($in_userlist)) {
            foreach($in_userlist as $v) {
                $this->get_user_role_dao()->update(array('role_id'=>$role_id),array('user_id'=>$v['user_id']));
                unset($user_list[$v['user_id']]);
            }
        }
        #2
        if(!empty($user_list)) {
            foreach($user_list as $uid) {
                $this->get_user_role_dao()->insert(array('user_id'=>$uid,'role_id'=>$role_id));
            }
        }

        return true;
    }

    /**
     * 设置用户特殊班别
     * @param unknown_type $role_id
     * @param unknown_type $user_list
     * @return boolean
     */
    public function set_users_type($user_type_id,$user_list) {
        $result = false;
        if(empty($user_type_id) || empty($user_list)) {
            return $result;
        }
        $user_type_id = $user_type_id==99 ? 0 : $user_type_id;
         if(!empty($user_list)) {
            foreach($user_list as $k => $v) {
                $this->get_userinfo_dao()->update(array('user_type'=>$user_type_id,'user_type_date'=>date('Ymd')),array('user_id'=>$v));
            }
        }
           return true;
     }

    //getdao
    private function get_loginlog_dao(){
        $dao = $this->get_dao('DAO_Login_LoginLog');
        return $dao;
    }
    private function get_userinfo_dao(){
        return $this->get_dao('DAO_Attendance_UserInfo');
    }

    /**
     * @return DAO_Attendance_UserRole
     */
    private function get_user_role_dao(){
        $dao = $this->get_dao('DAO_Attendance_UserRole');
        return $dao;
    }

    /**
     * @return Dao_Attendance_OrgRelation
     */
    private function get_org_relation_dao() {
        $dao = $this->get_dao('DAO_Attendance_OrgRelation');
        return $dao;
    }

    /**
     * @return Dao_Attendance_LeaveRecord
     */
    private function get_leave_record_dao() {
        $dao = $this->get_dao('Dao_Attendance_LeaveRecord');
        return $dao;
    }
}
